class CartItem < ApplicationRecord
  belongs_to :product, class_name: "StoreProduct", foreign_key: "store_product_id"
  # belongs_to :cart
  belongs_to :itemable, polymorphic: true

  scope :showable, -> { where(showable: true) }
  scope :exist_item, ->(product_id) { where(store_product_id: product_id).last }
  scope :customs, -> { select(&->(item){item.customable == true}) }

  delegate :url, :name, :format, :sale_price, :old_price, :customable, to: :product, allow_nil: true

  def amount
    quantity * sale_price
  end

  def unshow!
    self.update!(showable: false)
  end
end
